package com.example.api.service.impl;

import com.example.api.mapper.CommodityMapper;
import com.example.api.model.entity.Commodity;
import com.example.api.service.CommodityService;
import com.example.api.utils.DataTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Resource
    private CommodityMapper commodityMapper;

    @Override
    public Commodity save(Commodity commodity) {
        // 设置ID（如果为空）
        if (commodity.getId() == null || commodity.getId().isEmpty()) {
            commodity.setId(UUID.randomUUID().toString());
        }
        commodity.setCreateAt(DataTimeUtil.getNowTimeString());
        commodityMapper.insert(commodity);
        return commodity;
    }

    @Override
    public void update(Commodity commodity) {
        commodity.setUpdateAt(DataTimeUtil.getNowTimeString());
        commodityMapper.update(commodity);
    }

    @Override
    public void delete(String id) {
        commodityMapper.deleteById(id);
    }

    @Override
    public Commodity findById(String id) {
        return commodityMapper.findById(id);
    }

    @Override
    public List<Commodity> findAll() {
        return commodityMapper.findAll();
    }

    @Override
    public List<Commodity> findAllByLikeName(String name) {
        return commodityMapper.findByNameLike("%" + name + "%");
    }
}
